///////////////////////////////////////////////////////////////
/// $Id: link_list_test.cc 35 2009-10-12 10:04:29Z tonyjobmails $
/// $Date: 2009-10-12 10:04:29 +0000 (Mon, 12 Oct 2009) $
/// $Author: tonyjobmails $
/// $Revision: 35 $
/// E-mail: tonyjobmails@gmail.com
/// filename: operate_unit_test.cc
/// function: 链表的单元测试
///////////////////////////////////////////////////////////////

#include "linklist.h"
#include "character.h"

using namespace tototoy;
using namespace std;

int main()
{
	link_list<_character_type> list;
	_character_type c;
	op::action_print<_node_linklist<_character_type>*> print;	/// 执行接口


	cout << endl << "测试insert_front, 结果:";
	/// 非法操作测试
	list.delete_front();
	list.delete_back();
	list.delete_pos(1);
	list.delete_pos(2);
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试insert_front, 结果:";
	c.data = '1';
	/// 测试insert_front
	list.insert_front(c);
	/// 输出链表内所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试insert_back, 结果:";
	c.data = '2';
	/// 测试insert_back
	list.insert_back(c);
	/// 输出链表内所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试多次insert_pos, 结果:";
	/// 测试insert_pos
	c.data = '3';
	list.insert_pos(c, 1);
	c.data = '4';
	list.insert_pos(c, 5);
	c.data = '5';
	list.insert_pos(c, 3);
	c.data = '6';
	list.insert_pos(c, 5);
	/// 输出链表内所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:3 1 5 2 6
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试delete_front, 结果:";
	/// 测试delete_front
	list.delete_front();
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 5 2 6
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试delete_back, 结果:";
	/// 测试delete_front
	list.delete_back();
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 5 2
	cout << ", " << "链表长度:" << list.get_length();


	cout << endl << "测试delete_pos(5), 结果:";
	/// 测试delete_pos
	list.delete_pos(5);
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 5 2
	cout << ", " << "链表长度:" << list.get_length();
	cout << endl << "测试delete_pos(3), 结果:";
	list.delete_pos(3);
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 5
	cout << ", " << "链表长度:" << list.get_length();
	cout << endl << "测试delete_pos(1), 结果:";
	list.delete_pos(1);
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:5
	cout << ", " << "链表长度:" << list.get_length();


	c.data = '4';
	list.insert_front(c);
	c.data = '3';
	list.insert_front(c);
	c.data = '2';
	list.insert_front(c);
	c.data = '1';
	list.insert_front(c);
	/// 目前，链表中为:1 2 3 4 5
	cout << endl << "目前链表中元素为:";
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4 5
	cout << ", " << "链表长度:" << list.get_length();


	/// 测试delete_node();
	cout << endl << "测试delete_node() 删除元素为6, 结果:";
	c.data = '6';
	if(list.delete_node(c))
	{
		cout << "成功, 链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4 5
	cout << ", " << "链表长度:" << list.get_length();
	cout << endl << "测试delete_node() 删除元素为1, 结果:";
	c.data = '1';
	if(list.delete_node(c))
	{
		cout << "成功, 链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:2 3 4 5
	cout << ", " << "链表长度:" << list.get_length();
	cout << endl << "测试delete_node() 删除元素为5, 结果:";
	c.data = '5';
	if(list.delete_node(c))
	{
		cout << "成功, 链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:2 3 4
	cout << ", " << "链表长度:" << list.get_length();
	cout << endl << "测试delete_node() 删除元素为3, 结果:";
	c.data = '3';
	if(list.delete_node(c))
	{
		cout << "成功, 链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:2 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 得到第一个元素
	cout << endl << "测试get_front_elem()";
	if(list.get_front_elem(c))
	{
		cout << "成功，得到的元素为:" << c;			/// 正确输出为:2
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:4
	cout << ", " << "链表长度:" << list.get_length();


	/// 得到并删除第一个元素
	cout << endl << "测试get_front_elem_delete()";
	if(list.get_front_elem_delete(c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:2
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:4
	cout << ", " << "链表长度:" << list.get_length();


	/// 再次得到并删除第一个元素
	cout << endl << "测试get_front_elem_delete()";
	if(list.get_front_elem_delete(c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:4
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();


	/// 再次得到并删除第一个元素
	cout << endl << "测试get_front_elem_delete()";
	if(list.get_front_elem_delete(c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除pos上的元素
	cout << endl << "测试get_pos_elem(1)";
	if(list.get_pos_elem(1, c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();


	c.data = '4';
	list.insert_front(c);
	c.data = '3';
	list.insert_front(c);
	c.data = '2';
	list.insert_front(c);
	c.data = '1';
	list.insert_front(c);
	/// 目前，链表中为:1 2 3 4
	cout << endl << "目前链表中元素为:";
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除pos上的元素
	cout << endl << "测试get_pos_elem(5)";
	if(list.get_pos_elem(5, c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除pos上的元素
	cout << endl << "测试get_pos_elem(1)";
	if(list.get_pos_elem(1, c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:1
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除pos上的元素
	cout << endl << "测试get_pos_elem(2)";
	if(list.get_pos_elem(2, c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:2
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除pos上的元素
	cout << endl << "测试get_pos_elem(4)";
	if(list.get_pos_elem(4, c))
	{
		cout << "成功，得到的元素为:" << c << ", ";		/// 正确输出为:4
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();


	/// 查找结点
	cout << endl << "测试search_node(5)";
	_node_linklist<_character_type> *node;
	c.data = '5';
	node = list.search_node(c);
	if(node)
	{
		cout << "成功, 得到元素结点:" << node->data << ", ";	/// 正确输出为:
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();
	node = NULL;


	/// 查找结点
	cout << endl << "测试search_node(1)";
	c.data = '1';
	node = list.search_node(c);
	if(node)
	{
		cout << "成功, 得到元素结点:" << node->data << ", ";	/// 正确输出为:1
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();
	node = NULL;


	/// 查找结点
	cout << endl << "测试search_node(2)";
	c.data = '2';
	node = list.search_node(c);
	if(node)
	{
		cout << "成功, 得到元素结点:" << node->data << ", ";	/// 正确输出为:2
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();
	node = NULL;


	/// 查找结点
	cout << endl << "测试search_node(4)";
	c.data = '4';
	node = list.search_node(c);
	if(node)
	{
		cout << "成功, 得到元素结点:" << node->data << ", ";	/// 正确输出为:4
	}
	else
	{
		cout << "失败, ";
	}
	cout << "链表中元素为:";
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:1 2 3 4
	cout << ", " << "链表长度:" << list.get_length();
	node = NULL;

	/// 测试<<
	cout << "cout链表:" << list << endl;


	/// 删除整个链表
	cout << endl << "测试delete_list(), 结果:";
	if(list.delete_list())
	{
		cout << "成功，链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();


	/// 删除整个链表
	cout << endl << "再次测试delete_list(), 结果:";
	if(list.delete_list())
	{
		cout << "成功，链表元素为:";
	}
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);	/// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();

	/// 测试::insert_by_order
	cout << endl << "测试insert_by_order(1,2,3,4,5), 结果:";
	c.data = '2';
	list.insert_by_order(c);
	c.data = '3';
	list.insert_by_order(c);
	c.data = '4';
	list.insert_by_order(c);
	c.data = '1';
	list.insert_by_order(c, false);
	c.data = '5';
	list.insert_by_order(c);
	/// 输出链表中所有元素
	list.traver_node<_node_linklist<_character_type>*>(&print);     /// 正确输出为:
	cout << ", " << "链表长度:" << list.get_length();
	
	
	cout << endl;
	return 0;
}
